<script src="{{$smarty.const.__SHARE__}}js/jquery-1.8.3.min.js" type="text/javascript"></script>

<div class="map" id="map" style="width:800px;height:500px;margin:0 auto;"></div>
	
	<div style="padding:5px 10px;font-size:13px;">
		<input style="float:right;padding:8px 40px;font-size:15px;" onclick="parent.$('input[name=\'{{$smarty.get.name|default:'map'}}\']').val($('#mapval').html());parent.$.fn.colorbox.close();" value="确定坐标" type="button"/>

		<div>当前坐标：<a id="mapval">{{if $smarty.get.map}}{{$smarty.get.map}}{{else}}0,0{{/if}}</a></div>
		<input type="text" onkeydown="if(event.keyCode==13){event.keyCode=9;return false;}" value="" id="search"/>
		<input type="button" class="search_button" value="搜索" style="padding:1px 20px;"/>
	</div>

<script type="text/javascript">
$(document).ready(function(){
	loadScript();
});
function loadScript() { 
	var script = document.createElement("script"); 
	script.src = "https://api.map.baidu.com/api?v=2.0&ak=bBNVNmDlZ8Lj8zLBIVfdApHLmHXMY0yE&callback=initialize&s=1"; 
	document.body.appendChild(script); 
} 
function initialize() {
	var map = new BMap.Map("map"), // 创建地图实例
		marker = null;
		//////
		map.enableScrollWheelZoom();
		{{if $smarty.get.map}}
			var str='{{$smarty.get.map}}';
			str = str.split(',');
			var level = 10;
		{{else}}
			var str = '{{$smarty.get.default_point|default:"116.4,39.9"}}';
			str = str.split(',');
			var level = '{{$smarty.get.default_level|default:4}}';
		{{/if}}
		level = parseInt(level);
		var point = new BMap.Point(str[0], str[1]);
		$("#mapval").html(str[0]+","+str[1]);
		map.centerAndZoom(point, level);
		var marker = new BMap.Marker(point);
		map.addOverlay(marker);
		marker.enableDragging(true); // 设置标注可拖拽
		marker.addEventListener("dragend",function(e){
							$("#mapval").html(e.point.lng+","+e.point.lat);
						});
		//初始化城市搜索
		var local = new BMap.LocalSearch(map,{
			onSearchComplete:function(result){
				if(result && result.getPoi(0)){
					var point=result.getPoi(0).point;
					if(marker){
						//已有标记则设置标记位置
						marker.setPosition(point);//
					}
					else{
						//初始化标记
						marker=new BMap.Marker(point);
						map.addOverlay(marker);
						marker.enableDragging();
						marker.addEventListener("dragend",function(e){
							$("#mapval").html(e.point.lng+","+e.point.lat);
						});
					}
					$("#mapval").html(point.lng+","+point.lat);
					map.centerAndZoom(point,11);
				}
				
			}
		});
		
		$(".search_button").click(function(){//搜索框输入时
			local.search($('#search').val());
		});
		$('#search').keydown(function(e){
			if(e.keyCode==13)
				local.search($('#search').val());
		});
}
</script>